﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;

namespace FreeFlow.Administration
{
  /// <summary>
  /// A collection of tracking data for a workflow instance
  /// </summary>
  public sealed class WorkflowInstanceTrackingCollection : DatabaseAware, IList, IList<WorkflowInstanceTracking>
  {
    private Guid instanceGuid;
    internal WorkflowInstanceTrackingCollection(Server server, Guid instanceGuid)
      : base(server)
    {
      this.instanceGuid = instanceGuid;
    }

    private List<WorkflowInstanceTracking> trackingData;
    private void ReadTrackingData()
    {
      if (trackingData == null)
      {
        trackingData = new List<WorkflowInstanceTracking>();
        using (IDataReader reader = Server.ExecuteReader(
          string.Format("SELECT * FROM eMSWorkflowTracking WHERE eWorkflowInstance='{0}' ORDER BY eEventDateTime", 
          instanceGuid.ToString())))
        {
          while (reader.Read())
          {
            WorkflowInstanceTracking instanceEvent = new WorkflowInstanceTracking(reader);
            trackingData.Add(instanceEvent);
          }
        }
      }
    }

    #region IList Members

    int IList.Add(object value)
    {
      throw new NotImplementedException();
    }

    void IList.Clear()
    {
      throw new NotImplementedException();
    }

    bool IList.Contains(object value)
    {
      throw new NotImplementedException();
    }

    int IList.IndexOf(object value)
    {
      throw new NotImplementedException();
    }

    void IList.Insert(int index, object value)
    {
      throw new NotImplementedException();
    }

    bool IList.IsFixedSize
    {
      get { throw new NotImplementedException(); }
    }

    /// <summary>
    /// Gets a value indicating whether the <see cref="T:System.Collections.IList"/> is read-only.
    /// </summary>
    /// <value></value>
    /// <returns>true if the <see cref="T:System.Collections.IList"/> is read-only; otherwise, false.
    /// </returns>
    public bool IsReadOnly
    {
      get 
      {
        return true;
      }
    }

    void IList.Remove(object value)
    {
      throw new NotImplementedException();
    }

    void IList.RemoveAt(int index)
    {
      throw new NotImplementedException();
    }

    object IList.this[int index]
    {
      get
      {
        ReadTrackingData();
        return trackingData[index];
      }
      set
      {
        throw new NotImplementedException();
      }
    }

    #endregion

    #region ICollection Members

    /// <summary>
    /// Copies the elements of the <see cref="T:System.Collections.ICollection"/> to an <see cref="T:System.Array"/>, starting at a particular <see cref="T:System.Array"/> index.
    /// </summary>
    /// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from <see cref="T:System.Collections.ICollection"/>. The <see cref="T:System.Array"/> must have zero-based indexing.</param>
    /// <param name="index">The zero-based index in <paramref name="array"/> at which copying begins.</param>
    /// <exception cref="T:System.ArgumentNullException">
    /// 	<paramref name="array"/> is null.
    /// </exception>
    /// <exception cref="T:System.ArgumentOutOfRangeException">
    /// 	<paramref name="index"/> is less than zero.
    /// </exception>
    /// <exception cref="T:System.ArgumentException">
    /// 	<paramref name="array"/> is multidimensional.
    /// -or-
    /// <paramref name="index"/> is equal to or greater than the length of <paramref name="array"/>.
    /// -or-
    /// The number of elements in the source <see cref="T:System.Collections.ICollection"/> is greater than the available space from <paramref name="index"/> to the end of the destination <paramref name="array"/>.
    /// </exception>
    /// <exception cref="T:System.ArgumentException">
    /// The type of the source <see cref="T:System.Collections.ICollection"/> cannot be cast automatically to the type of the destination <paramref name="array"/>.
    /// </exception>
    public void CopyTo(WorkflowInstanceTracking[] array, int index)
    {
      ReadTrackingData();
      trackingData.CopyTo(array, index);
    }

    void ICollection.CopyTo(Array array, int index)
    {
      CopyTo((WorkflowInstanceTracking[])array, index);
    }

    /// <summary>
    /// Gets the number of elements contained in the <see cref="T:System.Collections.ICollection"/>.
    /// </summary>
    /// <value></value>
    /// <returns>
    /// The number of elements contained in the <see cref="T:System.Collections.ICollection"/>.
    /// </returns>
    public int Count
    {
      get
      {
        ReadTrackingData();
        return trackingData.Count;
      }
    }

    bool ICollection.IsSynchronized
    {
      get { throw new NotImplementedException(); }
    }

    object ICollection.SyncRoot
    {
      get { throw new NotImplementedException(); }
    }

    #endregion

    #region IEnumerable Members

    IEnumerator IEnumerable.GetEnumerator()
    {
      ReadTrackingData();
      return trackingData.GetEnumerator();
    }

    #endregion

    #region IList<WorkflowInstanceTracking> Members

    int IList<WorkflowInstanceTracking>.IndexOf(WorkflowInstanceTracking item)
    {
      throw new NotImplementedException();
    }

    void IList<WorkflowInstanceTracking>.Insert(int index, WorkflowInstanceTracking item)
    {
      throw new NotImplementedException();
    }

    void IList<WorkflowInstanceTracking>.RemoveAt(int index)
    {
      throw new NotImplementedException();
    }

    /// <summary>
    /// Gets the <see cref="FreeFlow.Administration.WorkflowInstanceTracking"/> at the specified index.
    /// </summary>
    public WorkflowInstanceTracking this[int index]
    {
      get
      {
        ReadTrackingData();
        return trackingData[index];
      }
      set
      {
        throw new NotImplementedException();
      }
    }

    #endregion

    #region ICollection<WorkflowInstanceTracking> Members

    void ICollection<WorkflowInstanceTracking>.Add(WorkflowInstanceTracking item)
    {
      throw new NotImplementedException();
    }

    void ICollection<WorkflowInstanceTracking>.Clear()
    {
      throw new NotImplementedException();
    }

    bool ICollection<WorkflowInstanceTracking>.Contains(WorkflowInstanceTracking item)
    {
      throw new NotImplementedException();
    }

    void ICollection<WorkflowInstanceTracking>.CopyTo(WorkflowInstanceTracking[] array, int arrayIndex)
    {
      throw new NotImplementedException();
    }

    bool ICollection<WorkflowInstanceTracking>.Remove(WorkflowInstanceTracking item)
    {
      throw new NotImplementedException();
    }

    #endregion

    #region IEnumerable<WorkflowInstanceTracking> Members

    /// <summary>
    /// Returns an enumerator that iterates through the collection.
    /// </summary>
    /// <returns>
    /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
    /// </returns>
    public IEnumerator<WorkflowInstanceTracking> GetEnumerator()
    {
      ReadTrackingData();
      return trackingData.GetEnumerator();
    }

    #endregion
  }
}
